#include "../lib/wiwannSoundTransmitter.cpp"
#include "../lib/wiwannTypes.h"

//package build-essential
#include <stdlib.h>
#include <vector>
#include <string.h>

#include <iostream>
#include <fstream>

#include <AL/alut.h>

//from package libopenal-dev
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alext.h>
//from package libalut-dev
#include <AL/alut.h>

extern "C"{
    #include <unistd.h>
}

bool wiwann_units_sound5()
{
    wiwannSoundTransmitter::transmittersInit(NULL);
    wiwannSoundTransmitter *wst;
    wst = wiwannSoundTransmitter::transmittersGetOne();
    while( wst )
    {
        std::cout << "\nokay\n " <<  wst;
        //sleep(1);
        
        wst = wiwannSoundTransmitter::transmittersGetOne();
    }
    delete wst ;

    std::cout << "source pas cree " ;

    delete wst ;
    wiwannSoundTransmitter::transmittersShutdown();
    std::cout << "sortie fonc" ;
}

bool wiwann_units_sound4()
{
    wiwannSoundTransmitter::transmittersInit(NULL);
    wiwannSoundTransmitter *wst;
    wst = wiwannSoundTransmitter::transmittersGetOne();
    if( wst )
    {
        std::cout << "okay " <<  wst;
    }
    else
    {
        std::cout << "source pas cree " ;
    }
    delete wst ;
    wiwannSoundTransmitter::transmittersShutdown();
    std::cout << "sortie fonc" ;
}


bool wiwann_units_sound1() { //transforme un fichier en gros buffer, le passe à un objet  wst pour 8 sec, et enqueue le buffer derriere
    //char* pCharTmpSize2;
    signed long int *audioBuffer;
    signed long int *audioBufferCur;
    long int sizeBuffer = 3000000;
    //TODO verifier que le fichier est bien lu en entier
    short int sizeRead = 1;

    ALuint hBuffer;
    ALenum format;
    ALsizei size;
    ALvoid* data;
    ALsizei freq;
    ALboolean loop;

    
    wiwannSoundTransmitter::transmittersInit(NULL);
    wiwannSoundTransmitter *wst;

    

    std::cout << "entree danbs la fubctiob";

    audioBuffer = (signed long int *) malloc(sizeof (signed long int) * sizeBuffer);

    std::cout << "ok pour le malloc";

    audioBufferCur = audioBuffer;

    std::cout << "ok recopie";

    std::ifstream myfile;
    myfile.open("wiwann/units/data/everybody16bmono.raw");



    if (myfile.is_open()) {
        std::cout << "ouverture du ficher:OK";
    } else {
        std::cout << "ouverture du fichier:KO";
        return false;
    }
    std::cout << "entree du chargement";
    while (!myfile.eof() && (audioBufferCur - audioBuffer) < sizeBuffer) {
        myfile.read((char*) audioBufferCur, sizeof (signed long int) * sizeRead);
        //std::cout << (char) *audioBuffer ;
        audioBufferCur = audioBufferCur + sizeRead;

    }
    std::cout << (audioBufferCur - audioBuffer) << "\n";

    myfile.close();
   wst = new wiwannSoundTransmitter();
    wst->setFormat(WIWANN_SOUND_FORMAT_MONO16);

    if(! wst->isPlaying())
    {
        std::cout << "\nca joue pas 1" ;
    }

    //wst.playData(2 * sizeBuffer, audioBufferBegin);
   wst->playData(440000, audioBuffer); //env 8 secondes

    sleep(1);

    if(! wst->isPlaying())
    {
        std::cout << "\nca joue pas 2" ;
    }
    else
    {
        std::cout << "\nca joue  2 :)" ;
    }
    wst->queueData(2 * sizeBuffer, audioBuffer);
    free(audioBuffer);
    std::cout << "\nca enfilade deuxieme buffer " << "\n" ;

     sleep(10);
     std::cout << "\ndestruction finale\n" ;
     delete wst ;
     wiwannSoundTransmitter::transmittersShutdown();
}



bool wiwann_units_sound2() //code openal minimal pour ouvrir un fichier
{
    alutInit(NULL, NULL);
    // Position of the listener.
    ALfloat ListenerPos[] = {0.0, 0.0, 0.0};

    // Velocity of the listener.
    ALfloat ListenerVel[] = {0.0, 0.0, 0.0};

    // Orientation of the listener. (first 3 elements are "at", second 3 are "up")
    ALfloat ListenerOri[] = {0.0, 0.0, -1.0, 0.0, 1.0, 0.0};

    ALuint hSource;

    alListenerfv(AL_POSITION, ListenerPos);
    alListenerfv(AL_VELOCITY, ListenerVel);
    alListenerfv(AL_ORIENTATION, ListenerOri);
    alGenSources(1, & hSource);
    alSourcei(hSource, AL_LOOPING, AL_FALSE);
    ALuint hBuffer = alutCreateBufferFromFile("data/everybody.wav");
    alSourceQueueBuffers(hSource, 1, &hBuffer);
    alSourcePlay(hSource);
}